package day4;

import java.util.zip.Inflater;

//克隆对象：
//浅克隆：需要实现一个Clone接口表示支持
//       重写父类的Clone()
//深克隆：需要实现一个Clone接口表示支持
//       重写父类的Clone() 对象属性也要这么做
public class Emp implements Cloneable{
    Integer id;
    String name;
    EmpInfo info;
    public Emp(){
        System.out.println("构造方法");
    }
    @Override
    protected Emp clone() throws CloneNotSupportedException {
        Emp e =  (Emp)super.clone();
        e.info = info.clone();
        return e;
    }
}
class EmpInfo implements Cloneable{
    Integer id;
    String sex;
    Integer age;

    @Override
    protected EmpInfo clone() throws CloneNotSupportedException {
        return (EmpInfo) super.clone();
    }
}
class TestClone{
    public static void main(String[] args) throws CloneNotSupportedException {
        Emp e = new Emp();
        e.id=100;
        e.name="xxy";
        e.info = new EmpInfo();
        e.info.id = 100;
        e.info.age=18;
        e.info.sex="n";
        Emp e2 = e.clone();//在堆内存直接复制一个对象出来
        System.out.println(e+" "+e.info);
        e.name="yyy";
        e.info.age=19;
        System.out.println(e2+" "+e2.info);
        System.out.println(e2.name+" "+e2.info.age);
    }
}
